package com.glsc.ngateway.platform.service.itflow.flowstrategy.equipmentServerApply.taskPostHandler;

import com.alibaba.excel.util.CollectionUtils;
import com.glsc.ngateway.common.api.flowable.dto.task.doTaskParam.equipment.EquipmentPassParamDto;
import com.glsc.ngateway.common.api.flowable.dto.task.doTaskParam.equipment.EquipmentRejectParamDto;
import com.glsc.ngateway.common.api.flowable.dto.task.formData.equipment.EquipmentServerApplyTaskFormDataDto;
import com.glsc.ngateway.common.api.flowable.dto.task.formData.equipment.domainDto.FlowFormEquipmentServerApplyDto;
import com.glsc.ngateway.platform.service.RoleService;
import com.glsc.ngateway.platform.service.itflow.flowstrategy.AbstractHandlerHelper;
import com.glsc.ngateway.common.base.enums.itflow.ITFlowTaskEnum;
import com.glsc.ngateway.common.base.enums.itflow.ITFlowTaskRoleEnum;
import io.jsonwebtoken.lang.Collections;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.time.LocalDate;
import java.util.List;

/**
 * 设备管理员审核
 *
 * @author xuchang
 */
@Component
public class ESAStep6DeviceAdminTaskPostHandlerAudit implements IEquipmentServerApplyTaskPostHandlerAudit {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Resource
    private RoleService roleService;

    @Autowired
    private ESAStepHandlerHelper handlerHelper;

    /**
     * 驳回后置处理方法
     *
     * @param passParam
     * @param operator
     */
    @Override
    public void rejectAfterHandle(@NotNull EquipmentRejectParamDto passParam, @NotNull String operator) {
        handlerHelper.afterHandle(passParam, operator);
    }

    @Override
    public void passAfterHandle(@NotNull EquipmentPassParamDto<FlowFormEquipmentServerApplyDto, EquipmentServerApplyTaskFormDataDto> param, @NotNull String operator) {
        /**
         * 将剩余信息补上
         */
        EquipmentServerApplyTaskFormDataDto data = param.getFlowableFormData();
        handlerHelper.fillOtherData(data);

        //抄送给信息安全岗
        List<String> accountList = roleService.getUsersByRole(ITFlowTaskRoleEnum.ROLE_INFO_SECURITY.getRoleName());
        if (Collections.isEmpty(accountList)) {
            logger.info("物理设备申领及上架流程-设备管理员审核-抄送:无抄送人");
        } else {
            LocalDate flowDesiredDate = data.getFormMainData().getFlowDesiredDate();
            handlerHelper.copyPorcess(param.getProcessId(), operator, accountList
                    , String.format("【抄送】-【物理设备申领及上架流程】-【%s】", data.getProcessTitle())
                    , AbstractHandlerHelper.GATEWAY_IT_FLOW_URL + "equipmentServerFlowAudit?flowProcessId=" + param.getProcessId()
                    , String.format("申请人：" + data.getFormMainData().getFlowCreateOperatorName() +
                            "<br/>  申领及上架物理设备：" +
                            (CollectionUtils.isEmpty(data.getFormServerDataList()) ? "0" : data.getFormServerDataList().size()) + "台" +
                            "<br/>  希望使用时间：" + flowDesiredDate.getYear() + "年" + flowDesiredDate.getMonthValue() + "月" + flowDesiredDate.getDayOfMonth() + "日" +
                            "<br/>  请知悉。可至国联技术服务平台内查阅相关事宜。"));
        }
    }

    @Override
    public String getTaskDefinitionKey() {
        return ITFlowTaskEnum.SERVER_APPLY_FLOW_DEVICE_ADMIN_TASK.getTaskKey();
    }
}
